package com.company;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.net.URI;

public class HttpHelper {

    private CloseableHttpClient httpClient;
    private CloseableHttpResponse httpResponse;

    public HttpHelper(){
        httpClient = HttpClients.createDefault();
    }
    public void close(){
        if ( httpResponse != null ){
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            httpClient = null;
            httpResponse = null;
        }
    }

    public HttpResponse getRequest(URI uri) throws IOException {
        HttpGet httpGet = new HttpGet(uri);
        httpResponse = httpClient.execute(httpGet);
        return httpResponse;
    }

    public HttpResponse postRequest(URI uri) throws IOException {
        HttpPost httpPost = new HttpPost(uri);
        //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("username", "vip"));
        //nameValuePairs.add(new BasicNameValuePair("password", "secret"));
        //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        httpResponse = httpClient.execute(httpPost);
        return httpResponse;
    }
}
